/**
 * Created by V5470-I5 on 2016/5/6.
 */
import java.net.*;
import java.io.*;
import javax.crypto.*;
import java.security.*;
import javax.crypto.Cipher;
public class MyClient {
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws Exception {
        KeyGenerator kg=KeyGenerator.getInstance("DESede");
        kg.init(168);
        SecretKey k=kg.generateKey( );
        FileOutputStream  f=new FileOutputStream("key1.dat");
        ObjectOutputStream b=new  ObjectOutputStream(f);
        b.writeObject(k);
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("请输入待发送的数据：");
        String srt=stdin.readLine();
        FileInputStream d=new FileInputStream("key1.dat");
        ObjectInputStream a=new ObjectInputStream(d);
        Key e=(Key)a.readObject( );
        Cipher cp=Cipher.getInstance("DESede");
        cp.init(Cipher.ENCRYPT_MODE, e);
        byte ptext[]=srt.getBytes("UTF8");
        for(int i=0;i<ptext.length;i++){
        }
        System.out.println("");
        byte ctext[]=cp.doFinal(ptext);
        for(int i=0;i<ctext.length;i++){
        }
        FileOutputStream f2=new FileOutputStream("SEnc.dat");
        f2.write(ctext);
        try {
            Socket socket = new Socket("172.20.10.4", 1105);
            Socket socket1 = new Socket("172.20.10.4", 1106);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
            PrintWriter out1=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
            System.out.print("读取文件中...\n");
            FileInputStream f1=new FileInputStream("key1.dat");
            FileInputStream f3=new FileInputStream("SEnc.dat");
            String str;
            out.println(f1);
            out1.println(f3);
            System.out.print("已发送\n");
            str=in.readLine();
            System.out.println( "从服务器接收到的结果为："+str);
        }
        catch (Exception v) {
            System.out.println(v);
        }
        finally{
        }
    }
}

